/*
 * @Description: 商品属性数据库操作
 * @Author: pinus
 * @Email: pinus0716@163.com
 * @Git: https://gitee.com/xyf97
 * @Date: 2021-10-08 15:58:34
 * @LastEditTime: 2021-10-09 21:36:57
 */

const path = require('path')
const daoModule = require('./DAO')
const databaseModule = require(path.join(process.cwd(), 'modules/database'))

/**
 * @description: 清除指定ID商品的所有属性
 * @param {*} goods_id
 * @param {*} cb
 * @return {*}
 */
module.exports.clearGoodAttributes = function (goods_id, cb) {
  const db = databaseModule.getDatabase()
  const sql = 'DELETE FROM sp_goods_attr WHERE goods_id = ?'
  db.query(sql, { replacements: [goods_id], type: db.QueryTypes.SELECT })
    .then(() => cb(null))
    .catch(err => cb('删除出错'))
}

/**
 * @description: 获得指定ID商品的所有属性
 * @param {*} goods_id
 * @param {*} cb
 * @return {*}
 */
module.exports.list = function (goods_id, cb) {
  const db = databaseModule.getDatabase()
  const sql = 'SELECT good_attr.goods_id,good_attr.attr_id,good_attr.attr_value,good_attr.add_price,attr.attr_name,attr.attr_sel,attr.attr_write,attr.attr_vals FROM sp_goods_attr as good_attr LEFT JOIN sp_attribute as attr ON attr.attr_id = good_attr.attr_id WHERE good_attr.goods_id = ?'

  db.query(sql, { replacements: [goods_id], type: db.QueryTypes.SELECT })
    .then(attrs => cb(null, attrs))
    .cathch(err => cb('删除出错-' + err))
}
